package com.headStorm.hutooluse;

import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.TemporalUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqq
 * @create 2020-12-05 10:18
 **/
public class TreeUseDemo {
    public static void main(String[] args) {
//        List<MyTreeObject> list = new ArrayList<>();
//
//
//        MyTreeObject treeObject11 = new MyTreeObject();
//        treeObject11.setId(0);
//        treeObject11.setParentId(-1);
//        treeObject11.setName("根根节点");
//
//        MyTreeObject treeObject1 = new MyTreeObject();
//        treeObject1.setId(1);
//        treeObject1.setParentId(0);
//        treeObject1.setName("根节点");
//
//        MyTreeObject treeObject2 = new MyTreeObject();
//        treeObject2.setId(2);
//        treeObject2.setParentId(1);
//        treeObject2.setName("子节点");
//
//        MyTreeObject treeObject3 = new MyTreeObject();
//        treeObject3.setId(3);
//        treeObject3.setParentId(2);
//        treeObject3.setName("孙子节点1");
//        treeObject1.setSort(1);
//
//
//        MyTreeObject treeObject4 = new MyTreeObject();
//        treeObject4.setId(4);
//        treeObject4.setParentId(2);
//        treeObject4.setName("孙子节点2");
//        treeObject1.setSort(2);
//
//
//        MyTreeObject treeObject5 = new MyTreeObject();
//        treeObject5.setId(5);
//        treeObject5.setParentId(2);
//        treeObject5.setName("孙子节点3");
//        treeObject1.setSort(3);
//
//
//        list.add(treeObject1);
//        list.add(treeObject2);
//        list.add(treeObject3);
//        list.add(treeObject4);
//        list.add(treeObject5);
//        list.add(treeObject11);
//
//
//
//        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
////        List<Tree<String>> list2 = TreeUtil.build(list,"0",treeNodeConfig,
////                (treeNode , tree) -> {
////                    tree.setId(treeNode.getId().toString());
////                    tree.setParentId(treeNode.getParentId().toString());
////                    tree.setWeight(treeNode.getSort());
////                    tree.setName(treeNode.getName());
////                    tree.putExtra("title", treeNode.getName());
////                    tree.putExtra("key", treeNode.getId());
////                });
//        // System.out.println(JSON.toJSONString(list2));
//
//        Tree<String> stringTree = TreeUtil.buildSingle(list, "-1", treeNodeConfig,
//                (treeNode, tree) -> {
//                    tree.setId(treeNode.getId().toString());
//                    tree.setParentId(treeNode.getParentId().toString());
//                    tree.setWeight(treeNode.getSort());
//                    tree.setName(treeNode.getName());
//                    tree.putExtra("title", treeNode.getName());
//                    tree.putExtra("key", treeNode.getId());
//                });
//
//
//        System.out.println();
//        System.out.println(JSON.toJSONString(stringTree));
//        System.out.println(JSON.toJSONString(stringTree.getChildren().get(0).getParentsName("2", false)));

        guanfangDemo();
    }

    public static void test(Parser parser){
        parser.parser();
        System.out.println("test");
    }

    public static void  guanfangDemo(){
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
        System.out.println(treeList.get(0).getParentsName("21", true));

    }


    public boolean test(){
        boolean flag  = false;
        if(flag){
            return false;
        }else {
            return true;
        }
    }


    /**
     * hutool 递归方法的模拟
     */
    public static void innerBuild(List<JSONObject> data , JSONObject parentNode){
        for (JSONObject childNode : data) {
            if(childNode.getStr("parentId").equals(parentNode.getStr("id"))){
                List children =(List) parentNode.get("children");
                if(children == null){
                    children = new ArrayList<JSONObject>();
                    parentNode.put("children" , children);
                }
                children.add(childNode);
            }
        }
    }
    /**
     * 模拟hutool 5.7.14 tree的实现 循环 + map 结构进行实现
     */

    public static void myTree(){
        Map<Integer, Map<String, Object>> idTreeMap = new HashMap<>();

        Map<String, Object> root = new HashMap<>();


        Map<String, Object> node1 = new HashMap<>();
        node1.put("id", 1);
        node1.put("pid", 0);
        node1.put("name", "根节点");

        Map<String, Object> node2 = new HashMap<>();
        node2.put("id", 2);
        node2.put("pid", 1);
        node2.put("name", "子节点");

        Map<String, Object> node3 = new HashMap<>();
        node3.put("id", 3);
        node3.put("pid", 2);
        node3.put("name", "孙子节点");

        idTreeMap.put(1, node1);
        idTreeMap.put(2, node2);
        idTreeMap.put(3, node3);

        for (Map<String, Object> node : idTreeMap.values()) {
            if (null == node) {
                continue;
            }
            root.put("child", node1);

            Map<String, Object> parentNode = idTreeMap.get(node.get("pid"));
            if (null != parentNode) {
                //parentNode.addChildren(node);
                Object child = parentNode.get("child");
                if(child == null){
                    parentNode.put("child", new ArrayList<>());
                }
                ((ArrayList) parentNode.get("child")).add(node);
            }
        }
        System.out.println(JSON.toJSONString(root.get("child")));
    }



}
